package com.laolei.core.redis;

import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.laolei.core.code.TransforCode;


public class LongRedisTemplate extends RedisTemplate<String, Long> {

	private final static RedisSerializer<String> stringSerializer = new StringRedisSerializer();
	private final static RedisSerializer<Long> longSerializer = new RedisSerializer<Long>() {
		public byte[] serialize(Long len) {
			return TransforCode.serialize256(len);
		}

		public Long deserialize(byte[] _bytes) {
			return TransforCode.deserialize256(_bytes);
		}
	};

	public LongRedisTemplate() {
		setKeySerializer(stringSerializer);
		setValueSerializer(longSerializer);
		setHashKeySerializer(stringSerializer);
		setHashValueSerializer(longSerializer);
	}

	public LongRedisTemplate(RedisConnectionFactory connectionFactory) {
		this();
		setConnectionFactory(connectionFactory);
		afterPropertiesSet();
	}

	protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
		return new DefaultStringRedisConnection(connection);
	}
}
